/*
*
* 球体缓冲区绘制(其上用三种颜色用于显示立体效果)
* R: 半径
* dTime: 递归次数
*
 */
function Ball_bufferInit(gl, R=1, dTime = 5, kind = 1) {

    //创建位置及法矢
    var positions = [];
    var normals = [];
    var index = 0;

    function tetrahedron(a, b, c, d, n) {
        divideTriangle(a, b, c, n);
        divideTriangle(d, c, b, n);
        divideTriangle(a, d, b, n);
        divideTriangle(a, c, d, n);
    }

    function divideTriangle(a, b, c, count) {
        if (count > 0) {
            var ab = normalize(mix(a, b, 0.5));
            var ac = normalize(mix(a, c, 0.5));
            var bc = normalize(mix(b, c, 0.5));

            divideTriangle(a, ab, ac, count - 1);
            divideTriangle(ab, b, bc, count - 1);
            divideTriangle(bc, c, ac, count - 1);
            divideTriangle(ab, bc, ac, count - 1);
        } else {
            positions = positions.concat(a).concat(b).concat(c);
            normals = normals.concat(a).concat(b).concat(c);
            index += 3;
        }
    }

    function mix(a, b, rate) {
        var v0 = a[0] * rate + b[0] * (1 - rate);
        var v1 = a[1] * rate + b[1] * (1 - rate);
        var v2 = a[2] * rate + b[2] * (1 - rate);

        return [v0, v1, v2];
    }

    function normalize(a) {
        var a0 = a[0];
        var a1 = a[1];
        var a2 = a[2];

        var len = Math.sqrt(Math.pow(a0, 2) + Math.pow(a1, 2) + Math.pow(a2, 2));
        var rate = R / len;
        return [a0 * rate, a1 * rate, a2 * rate];
    }

    var a = [R * 0, R * 0, R * -1];
    var b = [R * 0, R * 2 * Math.sqrt(2) / 3, R * 1 / 3];
    var c = [R * -Math.sqrt(6) / 3, R * -Math.sqrt(2) / 3, R * 1 / 3];
    var d = [R * Math.sqrt(6) / 3, R * -Math.sqrt(2) / 3, R * 1 / 3];

    tetrahedron(a, b, c, d, dTime);

    const positionBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuf);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

    const verticeNormalBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, verticeNormalBuf);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);

    //创建颜色
    const ballColor = [
        [0.3, 0.3, 0.7, 1.0],
        [0.2, 0.4, 0.6, 1.0],
        [0.4, 0.3, 0.5, 1.0],
        [0.3, 0.3, 0.7, 0.4],
    ];

    var faceColors = [];

    if (kind == 0) {
        for (var i = 0; i < index; i++) {
            faceColors = faceColors.concat(ballColor[0]);
        }
    } else if (kind == 1) {
        for (var i = 0; i < index / 3; i++) {
            faceColors = faceColors.concat(ballColor[0]);
            faceColors = faceColors.concat(ballColor[1]);
            faceColors = faceColors.concat(ballColor[2]);
        }
    } else {
        for (var i = 0; i < index; i++) {
            faceColors = faceColors.concat(ballColor[3]);
        }
    }

    const colorBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, colorBuf);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(faceColors), gl.STATIC_DRAW);

    //创建面单元
    var surfaces = [];

    for (var i = 0; i < index; i++) {
        surfaces.push(i);
    }

    const surfaceBuf = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, surfaceBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(surfaces), gl.STATIC_DRAW);

    var tp = false;

    if (kind == 2) {
        tp = true;
    }

    return {
        position: positionBuf,
        color: colorBuf,
        surface: surfaceBuf,
        normal: verticeNormalBuf,
        drawmode: gl.TRIANGLES,
        drawcount: index,
        transparent: tp,
    };
}